<?php
namespace app\ausadmin\controller;
use app\ausadmin\controller\Common;
use think\Request;
use think\Db;
use think\Controller;
use Godok\Org\Adminauth;
use think\Config;

class Group extends Common
{
    /**
     * 规则列表
     */
    
    public function index()
    {
        
        $keywords = Request::instance()->param("keywords",'');
        $query= Db::name( Config::get('auth.admin_group') )->field("id,title,status,description,listorder")->order('listorder asc');
        if ( !empty($keywords) ) {
            $query->where('title', 'like', '%'.$keywords.'%');
        }
        $data['list'] = $query->order("listorder asc,id asc")->limit(255)->select();
        
		return $this->fetch('index',$data);
    }
    /**
     * 绑定规则
     */
    public function rule($id='')
    {
        $id = Request::instance()->get('id','','number_int');
        
        if ( Request::instance()->isPost() ) {
            if (empty($id)) {
                return ['code'=>-1010, 'msg'=>lang('参数错误！')];
            }
            if (1 == $id ) {
                return ['code'=>-1020, 'msg'=>lang('系统内置权限组不能修改！')];
            }
            $data = ['rules'=> trim( Request::instance()->post('rules',''), ',') ];
            $data['id'] = $id;
            if (
                Db::name( Config::get('auth.admin_group') )
                ->update($data)
            ) { 
                $oid = 1;$m = 'ausadmin'; $c = 'Group'; $action="rule"; $uid=session('adminid'); $querystring = '绑定'.$id.'规则信息';
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
                return ['code'=>0, 'msg'=>lang('保存成功！')];
            } else {
                return ['code'=>-1030, 'msg'=>lang('保存失败！')];
            }
            
        } else {
            if (empty($id)) {
                return lang('参数错误！');
            }
            if (1 == $id ) {
                return lang('系统内置权限组不能修改！');
            }
            $data = ['id'=>$id];
            $group = Db::name(Config::get('auth.admin_group'))->where('id',$id)->find();
            $this->rules = explode(',', $group['rules']);//已绑定的规则
            $tree = \Godok\Org\Adminauth::menu();
            $tree[] = ['id'=>'all', 'pid'=>0, 'title'=>lang('所有权限'),'module'=>'','controller'=>'','action'=>'', 'ismenu'=>0];
            $data['menu'] = $this->menu2Html( $tree );
            return $this->fetch('rule',$data);
        }
        
    }
    /**
     * 格式化菜单
     */
    private  function menu2Html($list)
    {
        $menu = '';
        if( is_array($list) ) {
            $menu .= '<ol class="dd-list">';
            foreach ( $list as $li ) {
                $menu .= '<li class="dd-item ">';
                $menu .= '<div class="dd-handle "><div class="dd-nodrag">';
                if( !empty($li['icon']) ) {
                    $menu .= '<i class="' . $li['icon'] . '"></i> ';
                }
                if( in_array($li['id'], $this->rules) ) {
                    $menu .= '<span > <input name="rule[]" checked  type="checkbox" class="i-checks" value="'.$li['id'].'"> </span> ';
                } else {
                    $menu .= '<span > <input name="rule[]"  type="checkbox" class="i-checks" value="'.$li['id'].'"> </span> ';
                }
                
                $menu .= lang($li['title']);
                if ( $li['ismenu'] ) {
                    $menu .= ' ≡';
                }
                $menu .= '<span class="dd-tool-right"> <input name="selectChildren"  type="checkbox" class="i-checks-all" value="1">'. lang('全选').'</span> ';
                if ( !empty($li['module']) && !empty($li['controller']) && '*' != $li['module'] && '*' != $li['controller'] ) {
                    $url = \think\Request::instance()->root().'/'. $li['module'] . '/' . $li['controller'] . '/' . $li['action'];
                    if( !empty($li['condition']) ) {
                        $url .= '?' . $li['condition'];
                    }
                    $menu .= ' <em class="dd-url dd-nodrag">' . $url . '</em>';
                }
                $menu .= '</div></div>';

                if ( isset($li['children']) ) {
                    $menu .= $this->menu2Html($li['children']);
                }
                $menu .= '</li>';
            }
    
            $menu .= '</ol>';
            return $menu;
        } else {
            return '';
        }
    }
    /**
     * 添加权限组
     */
    public function add()
    {
        if ( Request::instance()->isPost() ) {
            $data = Request::instance()->only('listorder,status,description,title','post');
            if ( empty($data) ) {
                return ['code'=>-1010, 'msg'=>lang('参数错误！')];
            }

            if (
                Db::name( Config::get('auth.admin_group') )
                ->insert($data)
            ) {
                $data['id'] = Db::name(Config::get('auth.admin_group'))->getLastInsID();

//                $oid = 1;$m = 'ausadmin'; $c = 'Group'; $action="add"; $uid=session('adminid'); $querystring = '添加权限信息';
                $oid = 1;
                $m = 'ausadmin';
                $c = 'Group';
                $action="add";
                $uid=1;
                $querystring = '添加权限信息';
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
                return ['code'=>0, 'msg'=>lang('添加成功！'), 'data'=>$data];
            } else {
                return ['code'=>-1020, 'msg'=>lang('添加失败！')];
            }
        } else {
            $this->fetch();
        }
    }
    /**
     * 修改，内置管理员不能禁用
     * -----------------
     * /get  : view
     * -----------------
     * /post : update 
     * @$_POST[
     *  'id'       => 'id',
     *  'title' => '名称',
     *  'description' => '描述',
     *  'listorder'     => '排序编号',
     *  'status' => '状态'
     *  ]
     *  修改多条
     *  $_POST[{'id' => 'id','title'=>'名称'},{},{}]
     * -----------------
     * /call :
     * @param $id = int
     * @param $data = [
     *  'title' => '名称',
     *  'description' => '描述',
     *  'listorder'     => '排序编号',
     *  'status' => '状态'
     *  ]
     */
    public function edit($id='', $data= '')
    {
        if ( Request::instance()->isPost() ) {
            
            //do update
            if( empty($id) ){
                $id = Request::instance()->get('id','','number_int');
            }
            
            if ( empty($id) ){
                //批量修改
                $data = json_decode(file_get_contents('php://input'));
                if( is_array($data) ){
                    foreach ( $data as $item ) {
                        if ( is_object($item) && property_exists($item,'id') && !empty($item->id)) {
                            $column = [];
                            foreach(['title','description','listorder','status'] as $key) {
                                if( property_exists($item,$key) ) {
                                    $column[$key] = $item->$key;
                                }
                            }
                            if ( !empty($column) ) {
                                $this->edit($item->id, $column);
                            }
                        } else {
                            return ['code'=>-1010, 'msg'=>lang('提交数据错误！')];
                        }
                    }
                } else {
                    return ['code'=>-1020, 'msg'=>lang('提交数据错误！')];
                }
                $oid = 1;$m = 'ausadmin'; $c = 'Group'; $action="edit"; $uid=session('adminid'); $querystring = '批量修改权限组';
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
                return ['code'=>0, 'msg'=>lang('修改成功！')];
            } else {
                //修改单条
                if ( empty( $data) ) {
                    $data = Request::instance()->only('title,description,listorder,status','post');
                }
            
                if (isset($data['title']) && empty($data['title'])) {
                    return ['code'=>-1020, 'msg'=>lang('权限组名称不能为空！')];
                }
               
       
                if( $id == 1 && isset($data['status']) && 1 != $data['status']) {
                    return ['code'=>-1030, 'msg'=>lang('超级组不能禁用！')];
                }
                //内置3个用户组不能修改
                if ( $id < 4) {
                    if (isset($data['title']) ) {
                        unset($data['title']);
                    }
                    if (isset($data['description']) ) {
                        unset($data['description']);
                    }
                }
                if ( empty($data) ) {
                    return ['code'=>-1040, 'msg'=>lang('提交的数据错误！')];
                }
                //更新数据库
                if(
                    Db::name( Config::get('auth.admin_group') )
                    ->where('id',$id)
                    ->update($data)
                ) {
                $oid = 1;$m = 'ausadmin'; $c = 'Group'; $action="edit"; $uid=session('adminid'); $querystring = '单条修改'.$id.'权限组';
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
                    return ['code'=>0, 'msg'=>lang('修改成功！')];
                } else {
                    return ['code'=>-1060, 'msg'=>lang('修改失败！')];
                }
            }
        } else {
            //get view
            $id = Request::instance()->get('id','','number_int');
            if(empty($id)){
                return '$_GET["id"]不能为空！';
            }
            $group = Db::name(Config::get('auth.admin_group'))->where('id',$id)->find();
            $this->fetch('edit',['group'=>$group]);
        }
    }
    /**
     * 删除菜单
     * /post
     * @$_POST['id'] int
     */
    public function delete($id='')
    {
        $id = Request::instance()->get('id','','number_int');
    
        if (empty($id)) {
            return ['code'=>-1000, 'msg'=>lang('缺少参数id！')];
        }
        if (1 == $id) {
            return ['code'=>-1010, 'msg'=>lang('内置管理组不能删除！')];
        }
        if (2 == $id) {
            return ['code'=>-1020, 'msg'=>lang('登录用户组不能删除！')];
        }
        if (3 == $id) {
            return ['code'=>-1030, 'msg'=>lang('游客组不能删除！')];
        }
        $mygroups = Adminauth::getGroups(false,true);
        if (!in_array(1,$mygroups) && 1 != Adminauth::user('id') && !in_array($id, $mygroups)) {
            return ['code'=>-1040, 'msg'=>lang('无权限删除！')];
        }
        if (
            Db::name( Config::get('auth.admin_group') )
            ->where('id', $id)
            ->delete()
        ) {
            $oid = 1;$m = 'ausadmin'; $c = 'Group'; $action="delete"; $uid=session('adminid'); $querystring = '删除菜单'.$id;
                $ip = get_ip();$os = get_os();
                set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);    
            return ['code'=>0, 'msg'=>lang('删除成功！')];
        } else {
            return ['code'=>-1050, 'msg'=>lang('删除失败！')];
        }
    }

}
